﻿using System;
using System.Windows.Forms;
using Sunny.UI;
using System.ComponentModel;
using UIDATALayer;
namespace UserData
{
    public partial class Control_数据统计 : UserControl,Interface_UserControl
    {
        public Control_数据统计()
        {
            InitializeComponent();
        }
        string strName = "数据统计";
        [Category("数据统计")]
        public string 数据统计名称
        {
            set
            {
                strName = value;             
            }
            get { return strName; }
        }
        private UIDATALayer.UIDataAction<TestTimeData> acTestTimeData = null;
        private void Control_数据统计_Load(object sender, EventArgs e)
        {
            

        }
        public void Init()
        {
            TestTimeMgr.EvenReflushTestData += DelegateReflushData;
            TestTimeMgr.GetInstance().Init();
        }
        public void DelegateReflushData(string strName)
        {
            if(acTestTimeData==null)
            {
                UIDATALayerHelper.RegistAction<TestTimeData>(nameof(TestTimeData), ReflushData).Start();
                acTestTimeData = UIDATALayer.UIDATALayerHelper.GetRegistAction<TestTimeData>(nameof(TestTimeData));
                acTestTimeData.SetQueueNum(100);
            }
            if (strName==this.strName)
            {
                TestTimeData data = new TestTimeData();
                data.strName = strName;
                acTestTimeData.SetData(data);

            }
        }
        DateTime time_Last = DateTime.Now;
        private void ReflushData(TestTimeData data)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => ReflushData(data)));
            }
            else
            {
                if (data.strName != this.strName)
                    return;
                TestTime_数据统计 testTime = TestTimeMgr.GetInstance().dic_TestTime[strName];
                lb_OKCount.Text = testTime.n_OK.ToString();
                lb_NGCount.Text = (testTime.n_Total - testTime.n_OK).ToString();
                lb_TotalCount.Text = testTime.n_Total.ToString();
                lb_OKRate.Text = (((double)testTime.n_OK / (double)testTime.n_Total) * 100).ToString("0.00") + "%";
                lb_UPH.Text = (3600.0 * testTime.d_CTAllCount / testTime.d_CTAllTime).ToString("0.00");
                lbl_CurrentUPH.Text = (3600.0 / (DateTime.Now- time_Last).TotalSeconds).ToString("0.00");
                time_Last = DateTime.Now;
            }
        }

        private void 清除数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NGCountMgr.GetInstance().ClearNGCount();
            TestTimeMgr.GetInstance().Function_ClearData();
        }

        private void Menu_ClearDate_Opening(object sender, CancelEventArgs e)
        {

        }
    }
}
